//
//  HWGameClassifyProtocol.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/3/3.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit

class HWGameClassifyProtocol: NSObject {}

class HWGameClassifyLeftProtocol: NSObject,UITableViewDelegate,UITableViewDataSource {
    
    var models:[HWGameClassifyConfigModel]?
    var didSelectItem:((_ :Int)->())?
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        didSelectItem?(indexPath.row)
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return models?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = HWGameClassifyLeftCell.cellWithTableView(tableView: tableView)
        cell.model = models?[indexPath.row]
        return cell
    }
}

class HWGameClassifyRightProtocol: NSObject,UITableViewDelegate,UITableViewDataSource {
    var tempCell:HWGameClassifyGameCell!
    /// 是否为普通的cell
    var isNormal = false
    var model:HWClassifyModel?
    /// banner点击
    var didClickGame:((_ :String)->())?
    /// 播放
    var gameImageViewClick:((_:UIImageView,_:HWClassifyGameModel)->())?
    /// 结束播放
    var endPlay:(()->())?
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if isNormal == true {
            if indexPath.section == 0 {
                didClickGame?(model?.game.list[indexPath.row].id ?? "")
            } else {
                didClickGame?(model?.game.tuijian[indexPath.row].id ?? "")
            }
        } else {
            didClickGame?(model?.game.list[indexPath.row].id ?? "")
        }
        
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let header = HWGameClassifyHeader.headerOrFooterWithTableView(tableView: tableView)
        header.titleLabel.text = "精选推荐"
        if isNormal == true {
            if  section == 1 && (model?.game.tuijian.count ?? 0) > 0 {
                header.titleLabel.text = "为你推荐"
            }
        } else {
            
        }
        return header
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if isNormal == true {
            if  section == 1 && (model?.game.tuijian.count ?? 0) > 0 {
                return 40*widthScale
            }
            return 0
        } else {
            if section == 1 && (model?.game.list.count ?? 0) > 0{
                return 40*widthScale
            }else {
                return 0
            }
        }
        
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        if isNormal == true {
            return 2
        }else {
            return 2
        }
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if isNormal == true {
            if section == 0 {
                return model?.game.list.count ?? 0
            } else {
                return model?.game.tuijian.count ?? 0
            }
        } else {
            if section == 0 {
                if model?.slide.count ?? 0 > 0 {
                    return 1
                } else {
                    return 0
                }
            }else {
                return model?.game.list.count ?? 0
            }
        }
        
        
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let bannerCell = HWGameClassifyBannerCell.cellWithTableView(tableView: tableView)
        let gameCell = HWGameClassifyGameCell.cellWithTableView(tableView: tableView)
        let normalCell = HWGameClassifyNormalGameCell.cellWithTableView(tableView: tableView)
        if isNormal == true {
            if indexPath.section == 0 {
                normalCell.model = model?.game.list[indexPath.row]
            } else {
                normalCell.model = model?.game.tuijian[indexPath.row]
            }
            return normalCell
        }else {
            if indexPath.section == 0 {
                bannerCell.model = model
                bannerCell.didClickBanner = {
                    gameId in
                    self.didClickGame?(gameId)
                }
                return bannerCell
            }else {
                gameCell.model = model?.game.list[indexPath.row]
                gameCell.gameImageViewClick = {
                    self.gameImageViewClick?($0,$1)
                    self.tempCell = $2
                }
                return gameCell
            }
        }
        
    }
    
    func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        if self.tempCell == cell {
            self.endPlay?()
            self.tempCell = nil
        }
    }
}
